/**
 * 
 */
package praktikumid.w02.p3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * @author Aleksandr Lenin
 *
 */
public class UserInput {

	/**
	 * @param args arguments
	 */
	public static void main(String[] args) {
		try {
			inputFirst();
		} catch (IOException e) {
			System.err.println("Input cannot be read");
		}
	}
	
	/**
	 * Gets user input using an instance of a BufferedReader.
	 * @throws IOException input output exception
	 */
	public static void inputFirst() throws IOException {
		BufferedReader b = new BufferedReader(
				new InputStreamReader(System.in));
		
		while (true) {
			System.out.print("Enter a number: ");
			String str = b.readLine();
			
			if (str.equalsIgnoreCase("exit")) {
				break;
			}
			
			try {
				int nr = Integer.parseInt(str);
				System.out.println("The entered number is: " + nr);
			} catch (NumberFormatException e) {
				System.err.println("Not a number. Please try again.");
			}
		}
	}
	
	/**
	 * user input method using Scanner.
	 */
	public static void inputSecond() {
		Scanner s = new Scanner(System.in);
		
		while (true) {
			try {
				String str = s.nextLine();
				if (str.matches("^exit$")) {
					break;
				}
				int nr = Integer.parseInt(str);
				System.out.println("The entered number is: " + nr);
			} catch (NoSuchElementException e) {
				System.err.println("No next line was found");
			} catch (IllegalStateException e) {
				System.err.println("The scanner is closed");
				e.printStackTrace();
			} catch (NumberFormatException e) {
				System.err.println("Not a number. Please try again");
			}
		}
	}

}
